package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbProdAttaMapper;
import com.bwie.mapper.TbProdInfoMapper;
import com.bwie.pojo.TbProd;
import com.bwie.pojo.TbProdAtta;
import com.bwie.service.TbProdService;
import com.bwie.mapper.TbProdMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
* @author 牛y
* @description 针对表【tb_prod(商品表)】的数据库操作Service实现
* @createDate 2025-07-19 09:03:53
*/
@Service
public class TbProdServiceImpl extends ServiceImpl<TbProdMapper, TbProd>
    implements TbProdService{

    @Autowired
    TbProdMapper tbProdMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    TbProdAttaMapper tbProdAttaMapper;

    @Autowired
    TbProdInfoMapper tbProdInfoMapper;

    @Override
    public R getProdList(PageInfoVo pageInfoVo) {
        Page<TbProd> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<TbProd> prodList=tbProdMapper.getProdList(page,pageInfoVo);
        return R.OK(prodList);
    }

    @Override
    public R deleteByIds(List<TbProd> ids) {
        for (TbProd prod : ids) {
            tbProdMapper.deleteById(prod.getProdId());
        }
        return R.OK();
    }

    @Override
    public R addProd(TbProd tbProd, BindingResult result) {
        String token = request.getHeader("token");
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String userName = body.get("userName", String.class);
        if(result.hasFieldErrors()){
            return R.ERROR(500,result.getFieldError().getDefaultMessage());
        }
        TbProd prod = tbProdMapper.selectOne(new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdName, tbProd.getProdName()));
        if(prod!=null){
            return R.ERROR(500,"商品名称已存在,换一个试试呢");
        }
        tbProd.setProdStatus(0);
        tbProd.setCreateBy(userName);
        tbProd.setCreateTime(new Date());
        tbProdMapper.insert(tbProd);
        return R.OK();
    }

    @Override
    public R addProdAtta(TbProdAtta tbProdAtta) {
        TbProd prod = tbProdMapper.selectOne(new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdId, tbProdAtta.getProdId()));
        if(prod==null){
            return R.ERROR(500,"商品不存在");
        }
        String token = request.getHeader("token");
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String userName = body.get("userName", String.class);
        tbProdAtta.setCreateBy(userName);
        tbProdAtta.setCreateTime(new Date());
        tbProdAttaMapper.insert(tbProdAtta);
        return R.OK();
    }

    @Override
    public R getProdDetail(Integer prodId) {
        TbProd prod = tbProdMapper.selectById(prodId);
        if(prod==null){

        }
        return null;
    }
}




